package com.qiantu.phone.widget.setting;

import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.event.DeviceDetailChangeEvent;
import com.qiantu.phone.event.RefreshDeviceListEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class DeviceSettingShowInMainView extends LinearLayout {
    private final SwitchButton btnSetShowHomePageSwitch;
    private AppActivity mContext;
    private DeviceBean mDevice;
    ImageView iconDevice;

    public DeviceSettingShowInMainView(AppActivity context, DeviceBean bean) {
        this(context, bean, null);
    }

    public DeviceSettingShowInMainView(AppActivity context, DeviceBean bean, @Nullable AttributeSet attrs) {
        this(context, bean, attrs, 0);
    }

    public DeviceSettingShowInMainView(AppActivity context, DeviceBean bean, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, bean, attrs, defStyleAttr, 0);
    }

    public DeviceSettingShowInMainView(AppActivity context, DeviceBean bean, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        mDevice = bean;
        LayoutInflater.from(getContext()).inflate(R.layout.device_setting_showinmainview, this, true);
        int dp_16 = getResources().getDimensionPixelSize(R.dimen.dp_16);
        int dp_14 = getResources().getDimensionPixelSize(R.dimen.dp_14);
        setPadding(dp_16, dp_14, dp_16, dp_14);
        setGravity(Gravity.CENTER_VERTICAL);
        btnSetShowHomePageSwitch = findViewById(R.id.btn_home_page_show_switch);

        btnSetShowHomePageSwitch.setChecked(mDevice.getIsHomePage());

        btnSetShowHomePageSwitch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setShowHomePage();
            }
        });
    }

    private void setShowHomePage() {
        List<String> deviceSerialNoList = new ArrayList<>();
        deviceSerialNoList.add(mDevice.getDeviceSerialNo());
        HttpCallback<HttpData<Void>> callback = new HttpCallback<HttpData<Void>>(mContext) {
            @Override
            public void onSucceed(HttpData<Void> result) {
                if (result.isRequestSucceed()) {
                    mDevice.setIsHomePage(btnSetShowHomePageSwitch.isChecked());
                    LLDeviceDBManager.getInstance(getContext()).put(mDevice);
                    //2个事件都要发，首页会因为该修改去除，所以不能修改只设备状态
//                    EventBus.getDefault().post(new RefreshDeviceListEvent(mDevice.getRoomSerialNo(), mDevice.getIsFavourite()));
                    //如果首页的设备详情弹框显示的,那么会根据这个事件来隐藏
                    EventBus.getDefault().post(new DeviceDetailChangeEvent(mDevice.getDeviceSerialNo()));
                    mContext.toast(R.string.set_success);
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
                btnSetShowHomePageSwitch.setChecked(mDevice.getIsHomePage());
            }
        };
        if(mDevice.getDeviceGroup() == 0){
            LLHttpManager.deviceIsHomePage(mContext, deviceSerialNoList, btnSetShowHomePageSwitch.isChecked(), callback);
        }else{
            LLHttpManager.deviceGroupIsHomePage(mContext, deviceSerialNoList, btnSetShowHomePageSwitch.isChecked(), callback);
        }
    }
}
